package com.stalltea.algorithm.string;

/**
 * 字符串查找
 *
 * @author yivlchangfa
 * @since 2021/10/28 10:37
 */
public class FindString {

    public static void main(String[] args) {
        char[] s = { 'a', 'b', 'a', 'a' };
        char[] p = { 'a', 'a' };
        System.out.println(violent(s, p));
    }

    public static int violent(char[] s, char[] p) {
        int i = 0;
        int j = 0;
        int sLength = s.length;
        int pLength = p.length;

        while (i < sLength && j < pLength) {
            if (s[i] == p[j]) {
                i++;
                j++;
            } else {
                // 源字符串的下标
                i = i - j + 1;
                j = 0;
            }
        }
        if (j == pLength) {
            // 返回主串的下标
            return i - j;
        }
        return -1;
    }

}
